<?php
namespace Admin\Controller;

class AdminController extends Controller {

    public function index() {
        $m    = D2();
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        $this->assign('page', $page);
        $this->display();
    }

    public function read() {
        $id = I('get.id', 0, 'intval');

        try {
            if(empty($id)) {
                throw new ThinkException('参数不完整，无法获取数据');
            }

            $m = D2();

            if(!$record = $m->getByUid($id)) {
                throw new ThinkException('没有找到广告主客服信息');
            }
        } catch(Exception $e) {
            showJson(array('s' => false, 'm' => $e->getMessage()));
        }

        showJson(array('s' => true, 'd' => $record));
    }

    public function doSave() {
        $d = $_POST;
        $m = D2();

        try {
            $r = $m->save2($d)
                 ? array('s' => true, 'm' => '保存成功')
                 : array('s' => false, 'm'=> '保存出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

    public function del() {
        $id = I('get.id', 0, 'intval');

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整，无法删除数据'));
        }

        $m = D2();
        $r = $m->del($id) ? array('s' => true, 'm' => '删除成功') : array('s' => false, 'm' => '删除出错');

        showJson($r);
    }

}
